package com.danielagarza.app;

import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.danielagarza.app.modelos.Evento;
import com.danielagarza.app.servicios.LeerJSON;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class Home extends FragmentActivity {

	private SectionsPagerAdapter mSectionsPagerAdapter;
	private ViewPager mViewPager;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		// conectarFb();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			switch (position) {
			case 0:
				Portada portadaFragment = new Portada();
				return portadaFragment;
			case 1:
				// ProgressDialog pd =
				// ProgressDialog.show(this.getActivity(),"Twitter","Mensaje",true,
				// false);
				Noticias noticiasFragment = new Noticias();
				return noticiasFragment;
			case 2:
				Twitter twitterFragment = new Twitter();
				return twitterFragment;
			case 3:
				Agenda agendaFragment = new Agenda();
				return agendaFragment;
			case 4:
				Amigos amigosFragment = new Amigos();
				return amigosFragment;
			}

			return null;
		}

		@Override
		public int getCount() {
			return 5;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case 0:
				return getString(R.string.title_inicio);
			case 1:
				return getString(R.string.title_noticias);
			case 2:
				return getString(R.string.title_twitter);
			case 3:
				return getString(R.string.title_agenda);
			case 4:
				return getString(R.string.title_amigos);
			}
			return null;
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {
		case R.id.menu_eventos:
			CargarDatos t = new CargarDatos();
			t.execute();
			try {
				intent.putExtra("puntos",t.get());
				intent.setClass(Home.this, Mapa.class);
				startActivity(intent);
			} catch (JsonSyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return true;
		case R.id.menu_shake:
			intent.setClass(Home.this, Shake.class);
			startActivity(intent);
			return true;

		default:
			return false;
		}
	}

	
	 public void irInstagram(View view){
    	 try{
             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(LeerJSON.URL_INSTAGRAM));
             startActivity(intent);
         }catch (ActivityNotFoundException ex){
             Intent intent=new Intent(Intent.ACTION_VIEW,
                     Uri.parse(LeerJSON.URL_INSTAGRAM));
             startActivity(intent);
         }
    }
    
    public void irYouTube(View view){
    	try{
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(LeerJSON.URL_YOU_TUBE_CHANNEL));
            startActivity(intent);
        }catch (ActivityNotFoundException ex){
            Intent intent=new Intent(Intent.ACTION_VIEW,
                    Uri.parse(LeerJSON.URL_YOU_TUBE_CHANNEL));
            startActivity(intent);
        }
    	
    }
    
    public void irTwitter(View view){
    	try{
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(LeerJSON.URL_TWITTER_PERSONAL));
            startActivity(intent);
        }catch (ActivityNotFoundException ex){
            Intent intent=new Intent(Intent.ACTION_VIEW,
                    Uri.parse(LeerJSON.URL_TWITTER_PERSONAL));
            startActivity(intent);
        }	
    }
    
    private class CargarDatos extends AsyncTask<Void, Integer, String> {
		@Override
		protected String doInBackground(Void... arg0) {
			String valor = LeerJSON
					.leerJSON("http://li530-140.members.linode.com:9003/servicios/agenda");
			return valor;
		}
	}

 

}
